package com.pengtu.gsj.service.impl.check.gcjs;

import java.util.ArrayList;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.fastjson.JSONObject;
import com.pengtu.gsj.dao.base.QueryFilter;
import com.pengtu.gsj.dao.check.gcjs.NCRoadConstructionDao;
import com.pengtu.gsj.dao.system.AttachmentDao;
import com.pengtu.gsj.entity.app.Attachment;
import com.pengtu.gsj.entity.check.CheckTable;
import com.pengtu.gsj.entity.gcjs.NCRoadConstruction;
import com.pengtu.gsj.service.check.CheckTableService;
import com.pengtu.gsj.service.check.gcjs.RuralRoadConService;
import com.pengtu.gsj.service.system.SystemManageService;
import com.pengtu.gsj.utils.MathsUtils;

@Service
@Transactional
public class RuralRoadConServiceImpl implements RuralRoadConService{

	@Autowired
	private AttachmentDao attachmentDao;
	@Autowired
	private CheckTableService checkTableService;
	@Autowired
	private SystemManageService systemManageService;
	@Autowired
	private NCRoadConstructionDao ncRoadConstructionDao;
	
	@Transactional(readOnly = true)
	@Override
	public List<Attachment> getAttachmentList(QueryFilter fileter) {
		return attachmentDao.find(fileter);
	}

	@Override
	public List<NCRoadConstruction> getFieldOperations(QueryFilter queryFilter) {
		return ncRoadConstructionDao.find(queryFilter, "projectName", true);
	}

	@Override
	public Map<String, Object> getRuralRoadCons(String startTime, String endTime, CheckTable checkTable, String tag) throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();
		List<Object> values = new ArrayList<Object>();
		List<Attachment> attLists = new ArrayList<Attachment>();
		List<Double> scoLists = new ArrayList<Double>();
		List<NCRoadConstruction> ncRoads = new ArrayList<NCRoadConstruction>();
		
		JSONObject jsonObject = JSONObject.parseObject(checkTable.getRetrieval());
		String itemProjectId = (String)jsonObject.get("itemProjectId");
		double openScore = Double.parseDouble((String)jsonObject.get("firstStepScore"));
		double publicScore =  Double.parseDouble((String)jsonObject.get("secondStepScore"));
		double total =0; 			   //检查得分
		double percentage =0; 	      //系统完善度
		double tableFull = MathsUtils.mulDouble(openScore*7 + publicScore*3, 2);	//表总分
		
		//根据projectId和时间、标签查询是否有附件
		attLists = systemManageService.getAllCheckAttachments(startTime, endTime, tag);
		//如果id不为空，说明有条件，可以进行查询
		if (StringUtils.isNotEmpty(itemProjectId)) {
			//当检索条件不为空的时候更新分数
			values.add(itemProjectId);
			//如果不为空查出来的数据就加一个标志，可以根据这个标志来确定是否显示查询结果
			map.put("flag",true);
			//通过逗号将每个id切割出来，并搜索算分
			String[] idArrs = itemProjectId.split(",");
			for (int i = 0; i < idArrs.length; i++) {
				double tempSco = 0;
				//根据tag检索是否有附件
				if( i > 1) {
					break;
				}
				NCRoadConstruction ncRoad = ncRoadConstructionDao.get(idArrs[i]);
				ncRoads.add(ncRoad);
				// 记录是否得分，不得分为“0”，得分为“1”
				tempSco += ("1".equals(ncRoad.getConstructionPlan())) ? openScore : 0;
				tempSco += ("1".equals(ncRoad.getSubsidyPolicy())) ? openScore : 0;
				tempSco += ("1".equals(ncRoad.getTender())) ? openScore : 0;
				tempSco += ("1".equals(ncRoad.getConstructManager())) ? openScore : 0;
				tempSco += ("1".equals(ncRoad.getQualityControl())) ? openScore : 0;
				tempSco += ("1".equals(ncRoad.getFundUtilizing())) ? openScore : 0;
				tempSco += ("1".equals(ncRoad.getAcceptanceProject())) ? openScore : 0;
				tempSco += ("1".equals(ncRoad.getTransportBureau())) ? publicScore : 0;
				tempSco += ("1".equals(ncRoad.getCompetentDepartment())) ? publicScore : 0;
				tempSco += ("1".equals(ncRoad.getTownGovernment())) ? publicScore : 0;
				total += tempSco;
				scoLists.add(tempSco);
			}
		}
		percentage = MathsUtils.divDouble(total * 100, tableFull, 2);
		
		// 只有当系统完善度和检查得分不相等的情况下才更新数据 避免每次搜索都更新数据
		checkTableService.updateCheckTable(checkTable, percentage, total, tableFull, values);
		map.put("att", attLists);
		map.put("scoLists", scoLists);
		map.put("percent", percentage);
		map.put("manageData", ncRoads);
		map.put("score", checkTable.getTestFull());
		map.put("checkTable", checkTable);
		return map;
	}

	@Override
	public List<NCRoadConstruction> getRuralRoadConProject(QueryFilter queryFilter) {
		return ncRoadConstructionDao.find(queryFilter, "year", true);
	}
}
